package com.hpe.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.hpe.pojo.Jobentity;
import com.hpe.service.IEnterpriseInfoService;
import com.hpe.vo.JobInfo;

import sun.launcher.resources.launcher;
import com.hpe.util.HibernateUtil;

public class EnterpriseInfoServiceImpl implements IEnterpriseInfoService {

	//获取所有企业招聘信息（企业名称+招聘时间+岗位名称+岗位需求人数+地点+笔试人数+面试人数+offer数）
	@Override
	public List<JobInfo> queryJobInfoList(int pageno) {
		// TODO Auto-generated method stub
		String[] classes={"Enterpriseentity e","Jobentity j",};
		String[] fields={"e.entername","e.address","e.entertime","j.jobid","j.jobname","j.jobcount"};
		String condition="where e.enterid=j.enterid";
		List<Object[]> list=HibernateUtil.multiTableSelect(fields, classes, condition, pageno, 8);
		if (!list.isEmpty()) {
			List<JobInfo> list2=new ArrayList<JobInfo>();
			for (Object[] objects : list) {
				JobInfo jobInfo= new JobInfo();
				jobInfo.setEntername((String)objects[0]);
				jobInfo.setAddress((String)objects[1]);
				jobInfo.setEntertime((Timestamp)objects[2]);
				jobInfo.setJobid((Integer)objects[3]);
				jobInfo.setJobname((String)objects[4]);
				jobInfo.setJobcount((Integer)objects[5]);
				jobInfo.setwCount(wSumCount((Integer)objects[3]));
				jobInfo.setiCount(iSumCount((Integer)objects[3]));
				jobInfo.setOfferCount(oPassCount((Integer)objects[3]));
				list2.add(jobInfo);
			}
			return list2;
		}
		return null;
	}

	@Override
	public double wPassRate(int jobid) {
		// TODO Auto-generated method stub
		int sumNum=wSumCount(jobid);
		if (sumNum==0) {
			return -1;
		}
		int passNum=wPassCount(jobid);
		return 1.0*passNum/sumNum;
	}

	@Override
	public double iPassRate(int jobid) {
		// TODO Auto-generated method stub
		int sumNum=iPassCount(jobid);
		if (sumNum==0) {
			return -1;
		}
		int passNum=iPassCount(jobid);
		return 1.0*passNum/sumNum;
	}

	//企业的成功率
	@Override
	public double successRate(int jobid) {
		// TODO Auto-generated method stub
		Jobentity e=(Jobentity)HibernateUtil.get(Jobentity.class,jobid);
		int sumNum=e.getJobcount();
		if (sumNum==0) {
			return -1;
		}
		int passNum=oPassCount(jobid);
		return 1.0*passNum/sumNum;
	}

	@Override
	public List<JobInfo> queryEnterprise(String entername,int pageno) {
		// TODO Auto-generated method stub
		String[] classes={"Enterpriseentity e","Jobentity j",};
		String[] fields={"e.entername","e.address","e.entertime","j.jobid","j.jobname","j.jobcount"};
		String condition="where e.enterid=j.enterid and e.entername like '%"+entername+"%'";
		List<Object[]> list=HibernateUtil.multiTableSelect(fields, classes, condition, pageno, 8);
		if (!list.isEmpty()) {
			List<JobInfo> list2=new ArrayList<JobInfo>();
			for (Object[] objects : list) {
				JobInfo jobInfo= new JobInfo();
				jobInfo.setEntername((String)objects[0]);
				jobInfo.setAddress((String)objects[1]);
				jobInfo.setEntertime((Timestamp)objects[2]);
				jobInfo.setJobid((Integer)objects[3]);
				jobInfo.setJobname((String)objects[4]);
				jobInfo.setJobcount((Integer)objects[5]);
				jobInfo.setwCount(wSumCount((Integer)objects[3]));
				jobInfo.setiCount(iSumCount((Integer)objects[3]));
				jobInfo.setOfferCount(oPassCount((Integer)objects[3]));
				list2.add(jobInfo);
			}
			return list2;
		}
		return null;
	}

	@Override
	public int wPassCount(int jobid) {
		// TODO Auto-generated method stub
		return HibernateUtil.recordCount("*","Writtenentity w", "where w.jobid="+jobid+" and state=1");
	}

	@Override
	public int wSumCount(int jobid) {
		// TODO Auto-generated method stub
		return HibernateUtil.recordCount("*", "Writtenentity w", "where w.jobid="+jobid);
	}

	@Override
	public int iPassCount(int jobid) {
		// TODO Auto-generated method stub
		return HibernateUtil.recordCount("*","Interviewentity i", "where i.jobid="+jobid+" and state=1");
	}

	@Override
	public int iSumCount(int jobid) {
		// TODO Auto-generated method stub
		return HibernateUtil.recordCount("*", "Interviewentity i", "where i.jobid="+jobid);
	}

	@Override
	public int oPassCount(int jobid) {
		// TODO Auto-generated method stub
		return HibernateUtil.recordCount("*","Offerentity o", "where o.jobid="+jobid);
	}
}
